package com.example.myapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp.R;
import com.example.myapp.entity.ItemBean;
import com.example.myapp.entity.SolarTermBean;

import java.util.List;

//RecyclerView列表适配器

//Adapter从模型层获取数据，然后提供给RecyclerView显示，是沟通的桥梁。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder>{

    //上下文
    private Context mContext;
    //列表数据
    private List<ItemBean> mList;
    //
    private  LayoutInflater inflater;

    public MyRecyclerViewAdapter(Context context){
        this.mContext=context;
        //创建LayoutInflater对象
        inflater= LayoutInflater.from(mContext);
    }

    public void setList(List<ItemBean> mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

      //这个方法主要生成为每个Item inflater出一个View，
        //如果root不为null，attachToRoot设为false，则会将布局文件最外层的所有layout属性进行设置，当该view被添加到父view当中时，这些layout属性会自动生效。
        View view = inflater.inflate(R.layout.list_item_layout,parent,false);
        //创建ViewHolder
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.icon_img.setImageResource(mList.get(position).getImageInt());
        holder.title_tv.setText(mList.get(position).getTitle());
        holder.content_tv.setText(mList.get(position).getContent());
        holder.item_root.setOnClickListener((v)->{ //点击监听
            Toast.makeText(mContext,"点击"+mList.get(position).getTitle(),Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
  //创建ViewHolder和将模型层的数据绑 定到ViewHolder上
    class  MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView icon_img;
        private TextView title_tv;
        private TextView content_tv;
        private RelativeLayout item_root;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_img= itemView.findViewById(R.id.icon_img);
            title_tv=itemView.findViewById(R.id.title_tv);
            content_tv= itemView.findViewById(R.id.content_tv);
            item_root=itemView.findViewById(R.id.item_root);
        }
    }


}
